﻿/// <reference name="MicrosoftAjax.js"/>

Function.registerNameSpace("AGuang.Form");

AGuang.Form.FormBase = function() {
    this._baseElement=AGuang.Form.ParentLayer;
    this._autoSize=true;
    this._width=null;
    this._height=null;
    this._titleString="网页对话框";
    this._isExclusive=false;
    
    this._cssClass="AGuang____FormBase___DefaultStyle";
    this._maskClass="AGuang____FormBase___DefaultStyle____MaskStyle";
    this._minButtonAction={
        mouseover:EventOperate.createDelegate(this,this._minButtonMouseOver),
        mouseout:EventOperate.createDelegate(this,this._minButtonMouseOut),
        click:EventOperate.createDelegate(this,this._minButtonClick)
    };
    this._maxButtonAction={
        mouseover:EventOperate.createDelegate(this,this._maxButtonMouseOver),
        mouseout:EventOperate.createDelegate(this,this._maxButtonMouseOut),
        click:EventOperate.createDelegate(this,this._maxButtonClick)
    };
    this._closeButtonAction={
        mouseover:EventOperate.createDelegate(this,this._closeButtonMouseOver),
        mouseout:EventOperate.createDelegate(this,this._closeButtonMouseOut),
        click:EventOperate.createDelegate(this,this._closeButtonClick)
    };
    
    this._create();
}

AGuang.Form.FormBase.prototype = {
    initialize: function() {
    },
    dispose: function() {        
        //在此处添加自定义释放操作
        if(this._container){
            if(this._container.parentNode){
                this._container.parentNode.removeChild(this._container);
            }
            this._container=null;
        }
        if(this._header){
            EventOperate.clearEvents(this._head);
            this._head=null;
        }
        if(this._minButton){
            EventOperate.clearEvents(this._minButton);
            this._minButton=null;
        }
        if(this._maxButton){
            EventOperate.clearEvents(this._maxButton);
            this._maxButton=null;
        }
        if(this._closeButton){
            EventOperate.clearEvents(this._closeButton);
            this._closeButton=null;
        }
        this._head=null;
        this._title=null;
        this._left=null;
        this._right=null;
        this._icon=null;
        this._titleText=null;
    },
    set_defaultButton:function(value){
        this._defaultButton=value;
    },
    get_defaultButton:function(){
        return this._defaultButton;
    },
    set_maskClass:function(value){
        this._maskClass=value;
        if(this._bg) this._bg.className=value;
    },
    get_maskClass:function(){
        return this._maskClass;
    },
    set_isExclusive:function(value){
        this._isExclusive=value;
    },
    get_isExclusive:function(){
        return this._isExclusive;
    },
    set_autoSize:function(value){
        this._autoSize=value;
    },
    get_autoSize:function(){
        return this._autoSize;
    },
    set_width:function(value){
        if(!isNaN(value)) this._width=value+"px";
        else this._width=value;
        if(this._container){
            this._container.style.width=this.get_width();
			this._iframeBg.style.width=this.get_width();
            this._container.style.height=this.get_height();
            this._head.style.width=this.get_width();
            this._body.style.width=this.get_width();
            this._body_top.style.width=this.get_width();
            this._body_bottom.style.width=this.get_width();            
            this._body_top.style.width=this.get_width();
            var bodyHeight=(this._container.offsetHeight-this._head.offsetHeight)+"px";
            this._body.style.height=bodyHeight;
            this._body_left.style.height=bodyHeight;
            this._body_right.style.height=bodyHeight;
        }
    },
    get_width:function(){
        return this._width;
    },
    set_height:function(value){
        if(!isNaN(value)) this._height=value+"px";
        else this._height=value;
        if(this._container){
            this._container.style.height=this.get_height();
			this._iframeBg.style.height=this.get_height();
            var bodyHeight=(this._container.offsetHeight-this._head.offsetHeight)+"px";
            this._body.style.height=bodyHeight;
            this._body_left.style.height=bodyHeight;
            this._body_right.style.height=bodyHeight;
        }
    },
    get_height:function(){
        return this._height;
    },
    set_x:function(value){
        if(!isNaN(value)) this._x=value+"px";
        else this._x=value;
    },
    get_x:function(){
        return this._x;
    },
    set_y:function(value){
        if(!isNaN(value)) this._y=value+"px";
        else this._y=value;
    },
    get_y:function(){
        return this._y;
    },
    set_titleText:function(value){
        this._titleString=value;
        if(this._container){
            this._titleText.nodeValue=value;
        }
    },
    get_titleText:function(){
        return this._titleString;
    },
    _create:function(){
        this._bg=document.createElement("iframe");
        this._bg.className=this.get_maskClass();
        this._bg.style.border=0;
        this._bg.frameBorder=0;
        this._bg.oncontextmenu=function(){return false;}
        this._baseElement.appendChild(this._bg);
        EventOperate.addEvent(window,"resize",EventOperate.createDelegate(this,this._documentResize));

        this._container=document.createElement("div");
        this._container.className=this._cssClass;
        if(this._x) this._container.style.left=this._x+"px";
        if(this._y) this._container.style.top=this._y+"px";
        this._baseElement.appendChild(this._container);
		
		this._iframeBg=document.createElement("iframe");
		this._iframeBg.style.position="absolute";
		this._iframeBg.style.left="0";
		this._iframeBg.style.top="0";
		this._iframeBg.style.filter="alpha(opacity=0)";
		this._iframeBg.style.opacity=0;
		this._iframeBg.frameBorder=0;
		this._container.appendChild(this._iframeBg);
        EventOperate.addEvent(this._container,"keydown",EventOperate.createDelegate(this,this._clickDefaultButton));
        
        this._head=document.createElement("div");
        this._head.className="head";
        this._container.appendChild(this._head);
        EventOperate.addEvent(this._head,"mousedown",EventOperate.createDelegate(this,this._moveWin));
        
        this._left=document.createElement("div");
        this._left.className="left";
        this._head.appendChild(this._left);
        
        this._right=document.createElement("div");
        this._right.className="right";
        this._head.appendChild(this._right);
        
        this._title=document.createElement("div");
        this._title.className="title";
        this._head.appendChild(this._title);
        
        this._icon=new Image();
        this._icon.width=14;
        this._icon.height=14;
        this._icon.align="absmiddle";
        this._icon.style.display="none";
        this._title.appendChild(this._icon);
        
        this._titleText=document.createTextNode("");
        this._titleText.nodeValue=this.get_titleText();
        this._title.appendChild(this._titleText);
        
        this._minButton=document.createElement("div");
        this._minButton.className="minButton";
        EventOperate.addEvents(this._minButton,this._minButtonAction);
        this._head.appendChild(this._minButton);
        
        this._maxButton=document.createElement("div");
        this._maxButton.className="maxButton";
        EventOperate.addEvents(this._maxButton,this._maxButtonAction);
        this._head.appendChild(this._maxButton);
        
        this._closeButton=document.createElement("div");
        this._closeButton.className="closeButton";
        EventOperate.addEvents(this._closeButton,this._closeButtonAction);
        this._head.appendChild(this._closeButton);
        
        this._body=document.createElement("div");
        this._body.className="body";
        this._container.appendChild(this._body);
        
        this._body_top=document.createElement("div");
        this._body_top.className="top";
        this._body.appendChild(this._body_top);
        
        this._body_bottom=document.createElement("div");
        this._body_bottom.className="bottom";
        this._body.appendChild(this._body_bottom);
        
        this._body_left=document.createElement("div");
        this._body_left.className="left";
        this._body.appendChild(this._body_left);
        
        this._body_right=document.createElement("div");
        this._body_right.className="right";
        this._body.appendChild(this._body_right);
        
        this._body_leftTop=document.createElement("div");
        this._body_leftTop.className="leftTop";
        this._body.appendChild(this._body_leftTop);
        
        this._body_leftBottom=document.createElement("div");
        this._body_leftBottom.className="leftBottom";
        this._body.appendChild(this._body_leftBottom);
        
        this._body_rightTop=document.createElement("div");
        this._body_rightTop.className="rightTop";
        this._body.appendChild(this._body_rightTop);
        
        this._body_rightBottom=document.createElement("div");
        this._body_rightBottom.className="rightBottom";
        this._body.appendChild(this._body_rightBottom);
        
        this._contentBox=document.createElement("div");
        this._contentBox.style.padding="9px 8px";
        this._body.appendChild(this._contentBox);
    },
    addContentHTML:function(html){
        this.clearContent();
        this._contentBox.innerHTML=html;
    },
    addContent:function(obj){
        this.clearContent();
        this._contentBox.appendChild(obj);
    },
    clearContent:function(){
        this._contentBox.innerHTML="";
    },
    show:function(){
        var aHandler=EventOperate.getHandler(this,"showing");
        if(aHandler) {
            if(!aHandler(this,null)) return;
        }

        if(!this._container) this._create();
        if(this.get_isExclusive()) this._bg.style.display="";
		else this._bg.style.display="none";
        this._container.style.display="";
        this._bg.style.width=Math.max(document.getClientWidth(),document.getScrollWidth())+"px";
        this._bg.style.height=Math.max(document.getClientHeight(),document.getScrollHeight())+"px";
        if(this.get_isExclusive()){
			this._notFoucs(document);
			this._toFoucs(this._container);
		}
        this._container.focus();
        
        if(this.get_autoSize()){
            this._setAutoSize();
        }
        
        var aHandler=EventOperate.getHandler(this,"shown");
        if(aHandler) aHandler(this,null);

    },
    showAt:function(x,y){
        this.set_x(x);
        this.set_y(y);
        this.show();
    },
    showCenter:function(){
        this.show();
        document.toCenter(this._container);
    },
    hide:function(){
        var aHandler=EventOperate.getHandler(this,"hidding");
        if(aHandler) {
            if(!aHandler(this,Sys.EventArgs.Empty)) return;
        }
        this._toFoucs(document);
        if(this._container){
			this._container.style.display="none";
        	this._bg.style.display="none";
		}
        var aHandler=EventOperate.getHandler(this,"hiden");
        if(aHandler) aHandler(this,null);
    },
    reSize:function(width,height){
        this.set_autoSize(false);
        this.set_width(width);
        this.set_height(height);
        if(this._container){
            this._container.style.width=this.get_width();
            this._container.style.height=this.get_height();
            this._head.style.width=this.get_width();
            this._body.style.width=this.get_width();
            this._body_top.style.width=get_width();
            this._body_bottom.style.width=get_width();            
            this._body_top.style.width=width;
        }
    },
    moveTo:function(x,y){
        this.set_x(x);
        this.set_y(y);
        if(this._container){
            this._container.style.x=this.get_x();
            this._container.style.y=this.get_y();
        }
    },
    canReSize:function(isCan){
        if(!isCan){
            this._minButton.style.display="none";
            this._maxButton.style.display="none";
        }else{
            this._minButton.style.display="";
            this._maxButton.style.display="";
        }
    },
    add_showing:function(handler){
        EventOperate.addHandler(this,"showing",handler);
    },
    remove_showing:function(handler){
        EventOperate.addHandler(this,"showing",handler);
    },
    add_shown:function(handler){
        EventOperate.addHandler(this,"shown",handler);
    },
    remove_shown:function(handler){
        EventOperate.addHandler(this,"shown",handler);
    },
    add_hidding:function(handler){
        EventOperate.addHandler(this,"hidding",handler);
    },
    remove_hidding:function(handler){
        EventOperate.addHandler(this,"hidding",handler);
    },
    add_hiden:function(handler){
        EventOperate.addHandler(this,"hiden",handler);
    },
    remove_hiden:function(handler){
        EventOperate.addHandler(this,"hiden",handler);
    },
    _minButtonMouseOver:function(sender,e){
        e.stopPropagation();
        this._minButton.className="minButtonOver";
    },
    _minButtonMouseOut:function(sender,e){
        e.stopPropagation();
        this._minButton.className="minButton";
    },
    _minButtonClick:function(sender,e){
        e.stopPropagation();
        this._container.style.height=this._head.offsetHeight+"px";
    },
    _maxButtonMouseOver:function(sender,e){
        e.stopPropagation();
        this._maxButton.className="maxButtonOver";
    },
    _maxButtonMouseOut:function(sender,e){
        e.stopPropagation();
        this._maxButton.className="maxButton";
    },
    _maxButtonClick:function(sender,e){
        e.stopPropagation();
        this._container.style.height="";
    },
    _closeButtonMouseOver:function(sender,e){
        e.stopPropagation();
        this._closeButton.className="closeButtonOver";
    },
    _closeButtonMouseOut:function(sender,e){
        e.stopPropagation();
        this._closeButton.className="closeButton";
    },
    _closeButtonClick:function(sender,e){
        e.stopPropagation();
        this.hide();
    },
    _clickDefaultButton:function(sender,e){
        if(e.keyCode==13){
            if(this._defaultButton) this._defaultButton.click();
            e.stopPropagation();
            return false;
        }
    },
    _setAutoSize:function(){
        var headHeight=this._head.offsetHeight;
        var contentHeight=this._contentBox.offsetHeight;
        var contentWidth=this._contentBox.offsetWidth;
        var bodyHeight=this._body.offsetHeight;
        var bodyWidth=this._body.offsetWidth;
        if(bodyWidth<contentWidth){
            this._body.style.width=contentWidth+"px";
            this._container.style.width=contentWidth+"px";
        }
        if(bodyHeight<contentHeight){
            this._body.style.height=contentHeight+"px";
            this._container.style.height=(contentHeight+headHeight)+"px";
        }
    },
    _moveWin:function(sender,e){
        EventOperate.addMoveObj(this._container,e);
    },
    _documentResize:function(sender,e){
		setTimeout(function(){run();},100);
		oThis=this;
		function run(){
			oThis._bg.style.width=document.getScrollWidth()+"px";
			oThis._bg.style.height=document.getScrollHeight()+"px";
		}
		
    },
    _notFoucs:function(obj){
        nf(obj.getElementsByTagName("a"));
        nf(obj.getElementsByTagName("input"));
        nf(obj.getElementsByTagName("button"));
        nf(obj.getElementsByTagName("select"));
        nf(obj.getElementsByTagName("textarea"));
        function nf(objs){
            for(var i=0;i<objs.length;i++){
                if(objs[i].tabIndex){
                    if(objs[i].tabIndex!=-1){
                        objs[i].setAttribute("m______oldTabIndex",objs[i].tabIndex);
                        objs[i].tabIndex=-1;
                    }
                }else{
                    objs[i].tabIndex=-1;
                }
            }
        }
    },
    _toFoucs:function(obj){
        nf(obj.getElementsByTagName("a"));
        nf(obj.getElementsByTagName("input"));
        nf(obj.getElementsByTagName("button"));
        nf(obj.getElementsByTagName("select"));
        nf(obj.getElementsByTagName("textarea"));
        function nf(objs){
            
            for(var i=0;i<objs.length;i++){
                if(objs[i].getAttribute("m______oldTabIndex")!=""){
                    objs[i].tabIndex=objs[i].getAttribute("m______oldTabIndex");
                }
            }
        }
    }
}

AGuang.Form.MessageBox=function(msg,action){
    var f=new AGuang.Form.FormBase();
    f.set_isExclusive(true);
    f.set_height(120);
    f.canReSize(false);
    var box=document.createElement("div");
    f.addContent(box);
    
    var msgBox=document.createElement("div");
    msgBox.style.height="50px";
    msgBox.style.fontSize="12px";
    msgBox.innerHTML=msg;
    box.appendChild(msgBox);
    
    var buttonBox=document.createElement("div");
    buttonBox.style.textAlign="center";
    box.appendChild(buttonBox);
    
    var button=document.createElement("button");
    button.innerHTML="确定";
    f.set_defaultButton(button);

    buttonBox.appendChild(button);
    button.onclick=function(e){
        if(!e) e=event;
        EventOperate.cancelBubble(e);
        f.hide();
        f.dispose();
    }
    f.showCenter();

    if(action) f.add_hiden(action);
}

AGuang.Form.ShowLoadingBox=function(msg){
    var __msg="处理中，请稍候..."
    if(msg) __msg=msg;
    
    if(!AGuang["123456LoadingBox"]) {
        AGuang["123456LoadingBox"]=AGuang.Form._____Create__LoadingBox____();
    }
    var box=AGuang["123456LoadingBox"];
    box.msgNode.nodeValue=__msg;
    box.style.display="";
    document.toCenter(box);
}

AGuang.Form.HideLoadingBox=function(){
    if(AGuang["123456LoadingBox"]) AGuang["123456LoadingBox"].style.display="none";
}

AGuang.Form._____Create__LoadingBox____=function(){
    var box=document.createElement("div");
    box.className="AGuang____FormBase____loadingForm_____DefaultStyle";
    box.style.textAlign="center";
    AGuang.Form.ParentLayer.appendChild(box);
    EventOperate.addEvent(box,"mousedown",EventOperate.createDelegate(this,_moveWin));
    
    var loadingImageBox=document.createElement("div");
    loadingImageBox.className="loadingImage";
    //loadingImageBox.style.margin="0 auto";
    box.appendChild(loadingImageBox);
    
    box.msgNode=document.createTextNode("");
    box.appendChild(box.msgNode);
    
    function _moveWin(sender,e){
        EventOperate.addMoveObj(box,e);
        e.stopPropagation();
        return false;
    }
    return box;
}

AGuang.Form.ParentLayer=document.body;